package com.atguigu.gulimall.coupon.service.impl;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.coupon.dao.SeckillSessionDao;
import com.atguigu.gulimall.coupon.entity.SeckillSessionEntity;
import com.atguigu.gulimall.coupon.entity.SeckillSkuRelationEntity;
import com.atguigu.gulimall.coupon.service.SeckillSessionService;
import com.atguigu.gulimall.coupon.service.SeckillSkuRelationService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {

    @Autowired
    SeckillSkuRelationService skuRelationService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<SeckillSessionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SeckillSessionEntity> getSecKillOf3Days() {
        //开始时间
        String startTime = getStartTime();
        //结束时间
        String endTime = getEndTime();
        List<SeckillSessionEntity> sessionEntities = baseMapper.selectList(new QueryWrapper<SeckillSessionEntity>().between("start_time", startTime, endTime));
        List<SeckillSessionEntity> collect = sessionEntities.stream().map(session -> {
            //活动场次的编号
            Long id = session.getId();
            //该活动关联的商品
            List<SeckillSkuRelationEntity> relationEntities = skuRelationService.list(new QueryWrapper<SeckillSkuRelationEntity>().eq("promotion_session_id", id));
            session.setSkuRelationEntities(relationEntities);
            return session;
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * 获取开始的日期
     *
     * @return
     */
    public String getStartTime() {
        LocalDate nowDay = LocalDate.now();
        LocalTime min = LocalTime.MIN;
        LocalDateTime localDateTime = LocalDateTime.of(nowDay, min);
        String start = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return start;
    }

    /**
     * 获取结束的日期
     *
     * @return
     */
    public String getEndTime() {
        LocalDate nowDay = LocalDate.now();
        LocalDate plusDays = nowDay.plusDays(2);
        LocalTime min = LocalTime.MAX;
        LocalDateTime localDateTime = LocalDateTime.of(plusDays, min);
        String end = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return end;
    }


}